/**
 * 
 */
package com.kingcore.director.resourcemaster.ui;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingcore.director.main.MainFrame;

/**
 * @author Zeven
 *
 */
public class FavoriteFilesDropTarget extends DropTarget {

	private final static Logger log = Logger.getLogger(FavoriteFilesDropTarget.class);
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MainFrame mainFrame;

	public FavoriteFilesDropTarget(MainFrame mainFrame) {
		super();
		this.mainFrame = mainFrame;
	}

	@Override
    public synchronized void drop(DropTargetDropEvent dtde) {
        Point point = dtde.getLocation();
        //int column = table.columnAtPoint(point);
        //int row = table.rowAtPoint(point);
        // handle drop inside current table
        Object obj = dtde.getSource();
        //System.out.println("-------------2"+obj.getClass());
        dtde.acceptDrop( dtde.getDropAction() );//DropAction.ACTION_COPY_OR_MOVE
        Transferable transferable = dtde.getTransferable(); 
        DataFlavor[] flavors = transferable.getTransferDataFlavors(); 
      for (int i = 0; i < flavors.length; i++) { 
      DataFlavor d = flavors[i]; 
      //System.out.println("MIME type= " + d.getMimeType() + "\n "); 
     
        try { 
           
      if (d.equals(DataFlavor.javaFileListFlavor)) { 
      List<File> fileList = (List<File>) transferable.getTransferData(d); 
      log.info("MIME type= " + d.getMimeType() + "\n "); 
      String fileUri = null;
         for (File file : fileList) {
        	 log.info("---"+file.getAbsoluteFile());
        	 fileUri = file.getAbsolutePath();
				try {  //不采用批量处理，成功率低，如果一条有主键冲突则全部失败不好。
					this.mainFrame.getResourceMasterMainPanel()
							.getFavoriteFilesPanel().addNewFileToFavorite(fileUri, null);
				} catch (Exception e1) {
					e1.printStackTrace();
				}	
        }
      }
        }catch(Exception e){
            e.printStackTrace();
           
        }
      }
     
        //super.drop(dtde);
    }
//    
//	public FavoriteFilesDropTarget() {
//	    MyDropTarget mdt = new FavoriteFilesDropTarget();
//	    table.setModel(tm);
//	    table.setDropMode(DropMode.ON);
//	    //table.setDropTarget( mdt );
//	   scroll.setDropTarget(mdt);
//	    
//	   
//	    this.getContentPane().setLayout(new BorderLayout());
//	    this.getContentPane().add(scroll);
//	    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//	    this.setSize(800,600);
//	    this.setVisible(true);
//	}
	
	public static void main(String[] args) {
	    new FavoriteFilesDropTarget(null);
	}


}
